<?
/**
 * xml转化为数组
 * Author		peace@warmy.cn
 * Create Date	2008-03-04
 * Version		2.1
 */
class WXml2Array 
{ 
    protected  $xml=''; 
    function __construct() 
    { 
     	
    } 
    function load($source)
    {
    	$WFile 		= new WFile();
    	$this->xml	= $WFile->getfile($source);
		//print($this->xml);
    }
    function save($path)
    {
    	$wfile		= new WFile();
    	$wfile->write_to_file($path,$this->xml);
    }
    function loadXML($xml)
    {
    	$this->xml	= $xml;
    }
    function _struct_to_array($values, &$i) 
    {
        $child = array(); 
        if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']); 
         
        while ($i++ < count($values)) {
        	if(!isset($values[$i]['type'])) break;
            switch ($values[$i]['type']) { 
                case 'cdata': 
                array_push($child, $values[$i]['value']); 
                break;    
                case 'complete': 
                    $name = $values[$i]['tag']; 
                    if(!empty($name)){ 
                    if(isset($values[$i]['value'])) 
                    	$child[$name]	= $values[$i]['value'];
                    else
                    	$child[$name]	= "";
                    //$child[$name]= ($values[$i]['value'])?($values[$i]['value']):''; 
                    if(isset($values[$i]['attributes'])) {                     
                        $child[$name] = $values[$i]['attributes']; 
                    } 
                }     
              break; 
                case 'open': 
                    $name = $values[$i]['tag']; 
                    //$size = isset($child[$name]) ? sizeof($child[$name]) : 0; 
                    if(isset($child[$name]))
                    {
                    	print_r($child);
                    	print('存在了');
                    	exit;
                    }
                    $child[$name] = $this->_struct_to_array($values, $i); 
                break; 
                case 'close': 
                return $child; 
                break; 
            } 
        } 
        return $child; 
    }
    function createArray($to="utf-8",$from="utf-8") 
    { 
        $xml    = mb_convert_encoding($this->xml,$to,$from); 
        $values = array(); 
        $index  = array(); 
        $array  = array(); 
        $parser = xml_parser_create(); 
        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
        xml_parse_into_struct($parser, $xml, $values, $index); 
        xml_parser_free($parser); 
        $i = 0; 
        $name = $values[$i]['tag']; 
        $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : ''; 
        $array[$name] = $this->_struct_to_array($values, $i); 
        //$array = $this->encoding($array);
        return $array; 
    }
    private function encoding($value)
    {
		if(is_array($value))
		{
	    	foreach($value as $key => $val)
			{
				$value[$key] = $this->encoding($val);
			}
		}
		else 
			$value	= mb_convert_encoding($value,SYSTEM_CODE,"utf-8"); 
		return $value;
    }
    function __get($name)
    {
    	return $this->$name;
    }
    function __set($key,$value)
    {
    	$this->$key	= $value;
    }
     
}
?>